package com.envper.controller.device;

import com.envper.common.domain.dto.DeviceDto;
import com.envper.common.service.cls.ClassDeviceService;
import com.envper.mysql.domain.Result;
import com.envper.mysql.domain.Status;
import com.envper.common.service.device.DeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/device/info")
@RequiredArgsConstructor
public class InfoController {
    private final DeviceService deviceService;
    private final ClassDeviceService classDeviceService;

    @GetMapping("")
    public Result<DeviceDto> getDeviceInfo(@RequestParam("id") Long id) {
        DeviceDto deviceDto = deviceService.getDeviceInfoById(id);
        return Result.ok(Status.SUCCESS, deviceDto);
    }

    @GetMapping("/list")
    public Result<List<DeviceDto>> getDeviceInfoList(@RequestParam(name = "classId", required = false) Long classId) {
        List<DeviceDto> deviceDtoList;
        if (!ObjectUtils.isEmpty(classId)) {
            deviceDtoList = classDeviceService.getDeviceListByClassId(classId);
        } else {
            deviceDtoList = deviceService.getDeviceInfoList();
        }
        return Result.ok(Status.SUCCESS, deviceDtoList);
    }

    @PostMapping("/register")
    public Result<String> registerDeviceInfo(@RequestBody DeviceDto deviceDto) {
        deviceService.registerDeviceInfo(deviceDto);
        return Result.ok(Status.REGISTERSUCCESS);
    }

    @PostMapping("/remove/{id}")
    public Result<String> removeDeviceInfo(@PathVariable("id") Long id) {
        deviceService.removeDeviceInfo(id);
        return Result.ok(Status.REMOVESUCCESS);
    }
}
